// pages/add/add.js
var util = require('../../utils/util.js')
Page({
  data: {
    date: "2016-09-01",
    radioItems: [
      { name: '收入', value: '0', checked: true },
      { name: '支出', value: '1' }
    ],
    infos: [],
    m: 0
  },
  formSubmit: function (e) {
    var Thing = e.detail.value.thing;
    var Money;
    if (this.data.m == 0) {
      Money = "+ " + e.detail.value.money + "     ";
    } else {
      Money = "- " + e.detail.value.money + "     ";
    }
    this.data.infos = wx.getStorageSync('INFOS').infos;
    var info = [{
      thing: Thing,
      money: Money,
      time: this.data.date,
      desc: "无"
    }];
    if ((this.data.infos) != undefined) {
      this.data.infos = info.concat(this.data.infos);
    } else {
      this.data.infos = info;
    }

    wx.setStorageSync("INFOS", this.data);
    //wx.clearStorageSync("INFOS")
    wx.showToast({
      title: '添加成功',
      icon: 'success',
      duration: 1000,
      complete: function () {
        setTimeout(function () {
          wx.navigateTo({
            url: '../abook/abook'
          });
        }, 800);
      }
    });
  },
  onLoad: function (options) {
    this.setData({
      date: util.formatTime(new Date())
    });
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  radioChange: function (e) {
    var radioItems = this.data.radioItems;
    for (var i = 0, len = radioItems.length; i < len; ++i) {
      radioItems[i].checked = radioItems[i].value == e.detail.value;
    }

    this.setData({
      radioItems: radioItems,
      m: e.detail.value
    });
  },
})